//
//  MineMyPingJiaDDViewController.m
//  shopping
//
//  Created by 马田成 on 2017/9/28.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "MineMyPingJiaDDViewController.h"
#import "MineMyPingJDDTableViewCell.h"


#import "MineMyPingJiaHPTableViewCell.h"
#import "StarSliderA.h"
#import "MinePingJiaXingOne.h"
#define HeightVC [UIScreen mainScreen].bounds.size.height//获取设备高度
#define WidthVC [UIScreen mainScreen].bounds.size.width//获取设备宽度


@interface MineMyPingJiaDDViewController ()<numDelegate,UITableViewDelegate, UITableViewDataSource,UITextViewDelegate>
{

    int numStar;

    int numstar2;
    
    int numstar3;
    NSString *typePing;
    
    float buttonHeight;//图片高度
    
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property(nonatomic,strong) UIScrollView *mianScrollView;
@property (nonatomic, strong) UITextView *contentTV;
@property (nonatomic, strong) UILabel *placeHolderLabel;
@property (nonatomic, strong) NSMutableDictionary *productParamsss;

@property (nonatomic,strong) NSMutableArray * photoArr;

@property (nonatomic, strong) MineMyPingJiaHPTableViewCell *cell2;
@property (nonatomic, strong) StarSliderA *st1;
@property (nonatomic, strong) StarSliderA *st2;
@property (nonatomic, strong) StarSliderA *st3;


@end

@implementation MineMyPingJiaDDViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
    if ([[self getBigImageArray] count] < 3) {
        
        buttonHeight = ([UIScreen mainScreen].bounds.size.width - screenScale(45)) / 3;
        
    }else if ([[self getBigImageArray] count] < 6){
        
        buttonHeight = ([UIScreen mainScreen].bounds.size.width  - screenScale(45)) / 3 * 2 + screenScale(30);
        
    }else{
        
        buttonHeight = ([UIScreen mainScreen].bounds.size.width  - screenScale(45)) + screenScale(30);
        
    }
    [self.tableView reloadData];
    

    

    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creationView];
    [self regeditCell];
    typePing = @"1";
    // Do any additional setup after loading the view.
}
- (void)creationView{
    
    [self setTitle:@"添加评价"];
    
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64 - 44);
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;//去掉分割线
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    UIButton *buttonOfTJ = [[UIButton alloc] init];
    [self.view addSubview:buttonOfTJ];
    [buttonOfTJ mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.bottom.equalTo(self.view);
        make.height.offset(44);
        
    }];
    [buttonOfTJ addTarget:self action:@selector(tiJiaoCLick) forControlEvents:UIControlEventTouchUpInside];
    
    buttonOfTJ.backgroundColor = [UIColor colorWithRed:254 / 255.0f green:214 / 255.0f blue:54 / 255.0f alpha:1];
    [buttonOfTJ setTitle:@"提交" forState:UIControlStateNormal];
    
    [buttonOfTJ setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
}
- (void)tiJiaoCLick{

    
    self.photoArr = [[NSMutableArray alloc] initWithArray:[self getBigImageArray]];
    self.productParamsss = [[NSMutableDictionary alloc] init];
    
    NSString *strPage = [NSString stringWithFormat:@"%ld", (long)numStar];
    NSString *strPage2 = [NSString stringWithFormat:@"%ld", (long)numstar2];
    NSString *strPage3 = [NSString stringWithFormat:@"%ld", (long)numstar3];
    [self.productParamsss setValue:strPage forKey:@"goodsScore"];
    [self.productParamsss setValue:strPage2 forKey:@"timeScore"];
    [self.productParamsss setValue:strPage3 forKey:@"serviceScore"];

    [self.productParamsss setValue:_orderId forKey:@"orderId"];
    [self.productParamsss setValue:self.shopId forKey:@"shopId"];
    //   [self.productParamsss setValue:self.nameTF.text forKey:@"userName"];
    
    [self.productParamsss setValue:self.goodsId forKey:@"goodsId"];
    [self.productParamsss setValue:typePing forKey:@"ping"];
    
    
    [self.productParamsss setValue:self.contentTV.text forKey:@"content"];
    //    [self.productParams setValue:self.image forKey:@"pic"];
    
    
    //两种方式上传头像
    /*方式一：使用NSData数据流传图片*/
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/plain",
                                                         @"text/javascript",
                                                         @"text/json",
                                                         @"text/html",
                                                         @"image/jpeg", nil];
    [manager POST:adminLxlpersonaddappraise parameters:self.productParamsss constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if(self.photoArr){
            __block NSInteger i = 0;
            [self.photoArr enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                //压缩图片
                i++;
                // NSData *imageData = UIImagePNGRepresentation(obj);
                NSData *imageData = UIImageJPEGRepresentation(obj,0.005);
                
                
                //压缩图片
                
                //拼接图片名称+时间确保不重复命名
                NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                formatter.dateFormat = @"yyyyMMddHHmmss";
                NSString *imageFileName = [NSString stringWithFormat:@"%@%ld.jpg", [formatter stringFromDate:[NSDate date]],i];
                // 测试输出文件名
                
                //上传图片，以文件流的格式
                
                [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"image%ld",i] fileName:imageFileName mimeType:@"image/JPG"];

            }];
        }

        
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSString *strtmp = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSString *requestTmp = [NSString stringWithString: strtmp];
        NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];  //解析
        
        if ([[resultDic objectForKey:@"code"] intValue] == 1) {
            
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else {
            
            [PublicFunction showAlert:nil message:[resultDic objectForKey:@"msg"] duration:5.0f];
        }
        
        
        
        NSLog(@"+++>%@", resultDic);
        
        NSLog(@"--->%@", resultDic[@"msg"]);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"--->%@", error);
    }];

    

}
//代理赋值
- (void)getNum:(int)num{
    
    
    numStar = num;
    
    
    numstar2 = num;
    
    
    numstar3 = num;
    
    
    NSLog(@"AAAAAAAAAAA%d", numStar);
    
}



- (void)regeditCell{
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MineMyPingJDDTableViewCell" bundle:nil] forCellReuseIdentifier:@"MineMyPingJDDTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MineMyPingJiaHPTableViewCell" bundle:nil] forCellReuseIdentifier:@"MineMyPingJiaHPTableViewCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MinePingJiaXingOne" bundle:nil] forHeaderFooterViewReuseIdentifier:@"MinePingJiaXingOne"];
    
    
}
#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }else {
    
        return 50;
    }
}
#pragma mark - ---------- Section的内容 ----------
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    if (section == 0) {
        
        return nil;
        
    }else if (section == 1){
    
        MinePingJiaXingOne *sectionWork = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MinePingJiaXingOne"];
        
        sectionWork.leftLabel.text = @"描述相符";
        self.st1 = [[StarSliderA alloc] init];
        //代理
        self.st1.delegate = self;
        self.st1.tag = 1;
        
        [sectionWork addSubview:self.st1];
        [self.st1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(sectionWork.leftLabel.mas_centerY).with.offset(-3);
            make.left.equalTo(sectionWork.leftLabel.mas_right).with.offset(12);
            make.height.offset(30);
            make.right.equalTo(sectionWork).with.offset(-80);
            
        }];
        return sectionWork;
        
    
    }else if (section == 2){
        
        MinePingJiaXingOne *sectionWork = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MinePingJiaXingOne"];
        
        sectionWork.leftLabel.text = @"物流服务";
        self.st1 = [[StarSliderA alloc] init];
        //代理
        self.st1.delegate = self;
        self.st1.tag = 2;
        [sectionWork addSubview:self.st1];
        [self.st1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(sectionWork.leftLabel.mas_centerY).with.offset(-3);
            make.left.equalTo(sectionWork.leftLabel.mas_right).with.offset(12);
            make.height.offset(30);
            make.right.equalTo(sectionWork).with.offset(-80);
            
        }];
        return sectionWork;
        
        
    }else {
        
        MinePingJiaXingOne *sectionWork = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MinePingJiaXingOne"];
        
        sectionWork.leftLabel.text = @"服务态度";
        self.st1 = [[StarSliderA alloc] init];
        //代理
        self.st1.delegate = self;
        self.st1.tag = 3;
        
        [sectionWork addSubview:self.st1];
        [self.st1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(sectionWork.leftLabel.mas_centerY).with.offset(-3);
            make.left.equalTo(sectionWork.leftLabel.mas_right).with.offset(12);
            make.height.offset(30);
            make.right.equalTo(sectionWork).with.offset(-80);
            
        }];

        return sectionWork;

    }
    

}
//section 不随tableView滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.tableView)
        
    {
        
        CGFloat sectionHeaderHeight = 64; //header高度
        
        if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
            
        } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
            
        }
        
    }
    
    
    
}


#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    if (section == 0) {
        return 2;
    }else {
    
        return 0;
        
    }
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{


    if (indexPath.row == 0) {
        return 100 + buttonHeight;
    }else {
    
        return 100;
        
    }
    
}
#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
 
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            MineMyPingJDDTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MineMyPingJDDTableViewCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            for (UIView *view in cell.subviews) {
                
                [view removeFromSuperview];
                
            }
            
            UIImageView *imageView = [[UIImageView alloc] init];
            [cell addSubview:imageView];
            imageView.image = [UIImage imageNamed:@"dingdan_img"];
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(cell).with.offset(25);
                make.left.equalTo(cell).with.offset(15);
                make.width.offset(58);
                make.height.offset(58);
            }];
            self.contentTV = [[UITextView alloc] init];
            [cell addSubview:self.contentTV];
            self.contentTV.font = [UIFont systemFontOfSize:13];
            [self.contentTV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(cell).with.offset(25);
                make.left.equalTo(imageView.mas_right).with.offset(15);
                make.right.equalTo(cell.mas_right).with.offset(-15);
                make.height.offset(58);
                
            }];
            self.placeHolderLabel = [[UILabel alloc] init];
            
            self.placeHolderLabel.numberOfLines = 0;
            self.placeHolderLabel.font = [UIFont systemFontOfSize:13];
            self.placeHolderLabel.textColor = getColor(@"9B9B9B");
            [self.placeHolderLabel sizeToFit];
            self.placeHolderLabel.text = @"对我评价一下吧～";
            self.contentTV.delegate = self;
            
            
            [self.contentTV addSubview:self.placeHolderLabel];
            
            [self.contentTV setValue:self.placeHolderLabel forKey:@"_placeholderLabel"];
            
            
            _mianScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, screenScale(100), WidthVC, buttonHeight)];
            _mianScrollView.contentSize = CGSizeMake(WidthVC, buttonHeight);
            _mianScrollView.bounces = YES;
            _mianScrollView.showsVerticalScrollIndicator = false;
            _mianScrollView.backgroundColor = [UIColor whiteColor];
            [cell addSubview:_mianScrollView];
            [_mianScrollView setDelegate:self];
            self.showInView = _mianScrollView;
            self.maxCount = 9;
            
            [self initPickerView];
            
            return cell;

        }else {
        
            self.cell2 = [self.tableView dequeueReusableCellWithIdentifier:@"MineMyPingJiaHPTableViewCell" forIndexPath:indexPath];
            self.cell2.selectionStyle = UITableViewCellSelectionStyleNone;
            
            [self.cell2.haoPingBtn addTarget:self action:@selector(changeHaoPig) forControlEvents:UIControlEventTouchUpInside];
            [self.cell2.chaPingBtn addTarget:self action:@selector(changeChaPig) forControlEvents:UIControlEventTouchUpInside];
            [self.cell2.zhongPingBtn addTarget:self action:@selector(changezhongPig) forControlEvents:UIControlEventTouchUpInside];
            
            return self.cell2;
        
        }
        
    }else {
    
        return nil;
        
    }
    
    
}
- (void)changeHaoPig{

    typePing = @"1";
    self.cell2.haoPingLab.textColor = [UIColor redColor];
    self.cell2.zhongPingLab.textColor = [UIColor blackColor];
    self.cell2.chaPingLab.textColor = [UIColor blackColor];
    
    
}
- (void)changeChaPig{
    
    typePing = @"4";
    
    self.cell2.haoPingLab.textColor = [UIColor blackColor];
    self.cell2.zhongPingLab.textColor = [UIColor blackColor];
    self.cell2.chaPingLab.textColor = [UIColor redColor];
    
}
- (void)changezhongPig{
    typePing = @"2";
    self.cell2.haoPingLab.textColor = [UIColor blackColor];
    self.cell2.zhongPingLab.textColor = [UIColor redColor];
    self.cell2.chaPingLab.textColor = [UIColor blackColor];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
